#include "library.h"
#include <iostream>
using namespace std;

void Library::add_book(Book book)
{

    this->Books.push_back(book);
    cout << "Added:" << book.get_name() << endl;
}
/*#1
Added:Design_Patterns
*/
void Library::delete_book(int ID)
{
    for (auto it = Books.begin(); it != Books.end(); it++)
    {
        if (it->get_ID() == ID)
        {
            Books.erase(it);
            cout << "Deleted:" << it->get_name() << endl;
        }
        return;
    }
    cout << "Delete Failed" << endl;
}
/*#8
Deleted:Design_Patterns
*/

void Library::find_book(int ID)
{
    for (auto it = Books.begin(); it != Books.end(); it++)
    {
        if (it->get_ID() == ID)
        {
            cout << "ID:" << ID << endl;
            cout << "name:" << it->get_name() << endl;
            cout << "author:" << it->get_author() << endl;
            cout << "publishing_house:" << it->get_publishing_house() << endl;
            if (it->get_state() == 1)
            {
                cout << "state:loanable" << endl;
            }
            else
            {
                cout << "borrower:";
                it->print_message();
                cout << endl;
                cout << "state:borrowed" << endl;
            }
            return;
        }
    }
    cout << "Find Failed" << endl;
}
/*#2
ID:123456
name:Design_Patterns
author:John_Smith
publishing_house:Publisher_A
state:loanable
*/
void Library::borrow_book(int book_ID, string borrower)
{
    for (auto it = Books.begin(); it != Books.end(); it++)
    {
        if (it->get_ID() == book_ID)
        {
            // 被借出了
            if (it->get_state() == 0)
            {
                cout << "Borrow Failed" << endl;
                return;
            }
            else
            {
                it->change_state(borrower);
                cout << "Borrowed:" << it->get_name() << endl;
            }
            return;
        }
    }
    cout << "Borrow Failed" << endl;
}
void Library::return_book(int book_ID)
{
    for (auto it = Books.begin(); it != Books.end(); it++)
    {
        if (it->get_ID() == book_ID)
        {

            if (it->get_state() == 1)
            {
                cout << "Return Failed" << endl;
                return;
            }
            else
            {
                it->change_state();
                cout << "Returned:" << it->get_name() << endl;
            }
            return;
        }
    }
    cout << "Return Failed" << endl;
}
